﻿using Org.BouncyCastle.Crypto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Device;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Device
{
    public class MachineryTypeServices : IMachineryTypeServices
    {
        public MachineryTypeServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }

        public async Task<bool> AddAsync(MachineryType model)
        {
            //插入单条数据
            var result = await Db.InsertAsync(model);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            //批量删除数据
            string sql = @"
                        with tab as(
	                        select * from MachineryType where Id in @Ids
	                        union all 
	                        select child.* from MachineryType child join tab parent on parent.Id = child.ParentTypeId
                        )
                        delete MachineryType where id in(select id from tab)";
            var result = await Db.DeleteAsync(sql,new {Ids});
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            //删除一条数据
            string sql = @"
                        with tab as(
	                        select * from MachineryType where Id =@Id
	                        union all 
	                        select child.* from MachineryType child join tab parent on parent.Id = child.ParentTypeId
                        )
                        delete MachineryType where id in(select id from tab)";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<MachineryType>> GetAllAsync(string MachineryTypeCode, string MachineryTypeName)
        {
            string sql = "select * from MachineryType where EnableFlag='Y' ";
            if(!string.IsNullOrEmpty(MachineryTypeCode))
            {
                sql += "and MachineryTypeCode like @MachineryTypeCode";
            }
            if (!string.IsNullOrEmpty(MachineryTypeName))
            {
                sql += "and MachineryTypeName like @MachineryTypeName";
            }
            var list = await Db.QueryAsync<MachineryType>(sql, new { MachineryTypeCode=$"%{MachineryTypeCode}%", MachineryTypeName=$"%{MachineryTypeName}%" });
            return list;
        }
        public async Task<IEnumerable<MachineryType>> GetAllAsync()
        {
            string sql = "select * from MachineryType";
            var list = await Db.QueryAllAsync<MachineryType>(sql);
            return list;
        }

        public async Task<IEnumerable<MachineryType>> GetAsync(string MachineryTypeCode, string MachineryTypeName)
        {
            string sql = "select * from MachineryType where 1=1";
            if (!string.IsNullOrEmpty(MachineryTypeCode))
            {
                sql += "and MachineryTypeCode like @MachineryTypeCode";
            }
            if (!string.IsNullOrEmpty(MachineryTypeName))
            {
                sql += "and MachineryTypeName like @MachineryTypeName";
            }
            var list = await Db.QueryAsync<MachineryType>(sql, new { MachineryTypeCode = $"%{MachineryTypeCode}%", MachineryTypeName = $"%{MachineryTypeName}%" });
            return list;
        }


        public async Task<bool> UpdateAsync(MachineryType model)
        {
            var result =await Db.UpdateAsync(model);
            return result;
        }
    }
}
